//
//  WJDBManager.swift
//  PDFSign
//
//  Created by 李永彬 on 2025/9/19.
//

import Foundation
import FMDB

class WJDBManager {
    static let shared = WJDBManager()
    
    private var databaseConfigs: [WJDatabase: WJDatabaseConfig] = [:]
    
    private init() {
        registerAllDatabases()
    }
    
    // 初始化所有数据库
    private func registerAllDatabases() {
        for database in WJDatabase.allCases {
            let config = WJDatabaseConfig(database: database)
            databaseConfigs[database] = config
        }
    }
    
    // 获取或创建数据库配置
    func getOrCreateConfig(for database: WJDatabase) -> WJDatabaseConfig {
        if let config = databaseConfigs[database] {
            return config
        } else {
            let config = WJDatabaseConfig(database: database)
            databaseConfigs[database] = config
            return config
        }
    }
    
    // 更新数据库配置
    func updateDatabaseConfig(_ config: WJDatabaseConfig, for database: WJDatabase) {
        databaseConfigs[database] = config
    }
    
    // 初始化特定数据库
    func initializeDatabase(_ database: WJDatabase) throws {
        var config = getOrCreateConfig(for: database)
        
        if config.databaseQueue == nil {
            config.databaseQueue = FMDatabaseQueue(path: database.databasePath)
            databaseConfigs[database] = config
        }
        
        guard config.databaseQueue != nil else {
            throw WJDBError.databaseNotInitialized
        }
    }
    
    // 初始化所有数据库
    func initializeAllDatabases() throws {
        for database in WJDatabase.allCases {
            try initializeDatabase(database)
        }
    }
    
    // 关闭所有数据库
    func closeAllDatabases() {
        for database in WJDatabase.allCases {
            closeDatabase(database)
        }
    }
    
    // 关闭特定数据库
    func closeDatabase(_ database: WJDatabase) {
        if var config = databaseConfigs[database] {
            config.databaseQueue?.close()
            config.databaseQueue = nil
            databaseConfigs[database] = config
        }
    }
    
    // 备份数据库
    func backupDatabase(_ database: WJDatabase) throws {
        let fileManager = FileManager.default
        let backupPath = database.databasePath + ".backup"
        
        if fileManager.fileExists(atPath: database.databasePath) {
            try fileManager.copyItem(atPath: database.databasePath, toPath: backupPath)
        }
    }
    
    // 恢复数据库
    func restoreDatabase(_ database: WJDatabase) throws {
        let fileManager = FileManager.default
        let backupPath = database.databasePath + ".backup"
        
        if fileManager.fileExists(atPath: backupPath) {
            try fileManager.removeItem(atPath: database.databasePath)
            try fileManager.copyItem(atPath: backupPath, toPath: database.databasePath)
        }
    }
    
    // 获取所有数据库信息
    func getAllDatabases() -> [WJDatabase] {
        return WJDatabase.allCases
    }
    
    // 获取数据库状态
    func getDatabaseStatus(_ database: WJDatabase) -> (isOpen: Bool, path: String) {
        if let config = databaseConfigs[database], config.databaseQueue != nil {
            return (true, database.databasePath)
        } else {
            return (false, database.databasePath)
        }
    }
    
    // 删除数据库文件
    func deleteDatabaseFile(_ database: WJDatabase) throws {
        closeDatabase(database)
        
        let fileManager = FileManager.default
        if fileManager.fileExists(atPath: database.databasePath) {
            try fileManager.removeItem(atPath: database.databasePath)
        }
    }
    
    // 获取数据库大小
    func getDatabaseSize(_ database: WJDatabase) -> Int64? {
        let fileManager = FileManager.default
        do {
            let attributes = try fileManager.attributesOfItem(atPath: database.databasePath)
            return attributes[.size] as? Int64
        } catch {
            return nil
        }
    }
}
